package com.rui.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rui.reggie.common.R;
import com.rui.reggie.entity.Employee;
import com.rui.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author : [锐神NO.3]
 * @version : [v1.0]
 * @className : EmployeeController
 * @createTime : [2022/5/14 23:08]
 * @description : [员工管理控制器]
 */

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //1.将页面提交的密码进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据页面提交的用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        //3.判断用户是否存在
        if(emp == null) {
            return R.error("用户名不存在,登录失败");
        }

        //4.判断密码是否正确
        if(!emp.getPassword().equals(password)) {
            return R.error("密码错误,登录失败");
        }

        //5.查看员工状态，如果为已禁用状态，则返回员工已禁用的结果
        if(emp.getStatus() == 0) {
            return R.error("员工已禁用,登录失败");
        }

        //6.将员工信息放入session中, 并返回登录成功的结果
        request.getSession().setAttribute("employee", emp.getId());

        return R.success(emp);
    }

    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //清理session中保存的当前登录的员工的id
        request.getSession().removeAttribute("employee");

        return R.success("退出成功");
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        //控制台查看当前新增的员工信息
        log.info("新增员工, 员工信息{}", employee.toString());

        //设置初始密码，需要进行md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //以下内容由mybatisplus提供的公共字段自动填充功能已实现自动注入
        /*//设置创建时间
        employee.setCreateTime(LocalDateTime.now());

        //设置修改时间
        employee.setUpdateTime(LocalDateTime.now());

        //获得当前登录的员工id
        Long empId = (Long) request.getSession().getAttribute("employee");

        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/

        employeeService.save(employee);

        return R.success("新增员工成功");
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //控制台查看当前查询的员工信息
        log.info("page = {}, pageSize = {} , name = {}", page, pageSize, name);

        //构造分页器
        Page pageInfo = new Page(page, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();

        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);

        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行查询
        employeeService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 根据id修改员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee, HttpServletRequest request) {
        //控制台查看当前修改的员工信息
        log.info(employee.toString());

        //以下内容由mybatisplus提供的公共字段自动填充功能已实现自动注入
        /*//获取当前登录的员工id, 确定是谁在修改
        Long empId = (Long) request.getSession().getAttribute("employee");

        //设置修改时间
        employee.setUpdateTime(LocalDateTime.now());

        //设置修改人
        employee.setUpdateUser(empId);*/

        //执行修改
        employeeService.updateById(employee);

        return R.success("修改员工信息成功");
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        log.info("根据id查询员工信息...");

        //根据id查询员工
        Employee employee = employeeService.getById(id);

        if(employee != null) {
            return R.success(employee);
        }else {
            return R.error("没有查询到对应员工信息");
        }
    }
}
